package ferp.android.dialogs;

import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import ferp.android.GUI;
import ferp.android.R;

/**
 * User: olegs
 * Date: 30/05/12 15:13
 */
public class EditTextDialog extends Dialog
{
  public interface Listener
  {
    void onSaveClick(int requestCode, String value);
  }

  public EditTextDialog(Context context, Listener listener, int requestCode)
  {
    super(context, R.layout.dialog_edit_text);

    // set listener
    this.listener = listener;
    this.requestCode = requestCode;

    // always show keyboard
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

    // resolve edit text view
    editText = (EditText)findViewById(R.id.dlg_edit_text);
    editText.setTypeface(GUI.Font.BOLD);

    // set 'save' button listener
    resolveButton(R.id.dlg_element_footer_button, R.string.save).setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View view)
      {
        EditTextDialog.this.listener.onSaveClick(EditTextDialog.this.requestCode, editText.getText().toString().trim());
      }
    });
  }

  public void set(String title, String text, int inputType)
  {
    this.title.setText(title);
    editText.setInputType(inputType);

    if (text == null)
      text = "";

    editText.setText(text);
    editText.setSelection(0, text.length());
  }

  // listener
  private final Listener listener;
  // request code
  private final int requestCode;
  // widgets
  private final EditText editText;
}